package com.koron.order.patrol.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.order.patrol.bean.po.PatrolEnvCheckPo;
import com.koron.order.patrol.mapper.PatrolEnvCheckMapper;
import com.koron.order.patrol.service.PatrolEnvCheckService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 巡视工单-巡视点_巡视环境_检查项从表(PatrolEnvCheck)表服务实现类
 *
 * @author zhouj
 * @since 2021-10-26 15:24:47
 */
@Service("patrolEnvCheckService")
public class PatrolEnvCheckServiceImpl implements PatrolEnvCheckService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, PatrolEnvCheckPo patrolEnvCheck) {

        PatrolEnvCheckMapper mapper = factory.getMapper(PatrolEnvCheckMapper.class);

        return mapper.insertOrUpdate(patrolEnvCheck);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<PatrolEnvCheckPo> patrolEnvCheckList) {

        if (CollectionUtils.isEmpty(patrolEnvCheckList)) {
            return 0;
        }
        
        Assert.notEmpty(patrolEnvCheckList, "巡视环境不能为空");

        List<String> patrolPointIds = patrolEnvCheckList.stream().map(PatrolEnvCheckPo::getPatrolPointId).distinct().collect(Collectors.toList());

        Assert.notEmpty(patrolPointIds, "巡视点id不能为空");

        PatrolEnvCheckMapper mapper = factory.getMapper(PatrolEnvCheckMapper.class);

        //先根据 巡视点ids删除
        mapper.deleteByPatrolPointIds(patrolPointIds);

        return mapper.insertBatch(patrolEnvCheckList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, PatrolEnvCheckPo patrolEnvCheck) {

        PatrolEnvCheckMapper mapper = factory.getMapper(PatrolEnvCheckMapper.class);

        return mapper.insert(patrolEnvCheck);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<PatrolEnvCheckPo> patrolEnvCheckList) {

        PatrolEnvCheckMapper mapper = factory.getMapper(PatrolEnvCheckMapper.class);

        return mapper.insertBatch(patrolEnvCheckList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, PatrolEnvCheckPo patrolEnvCheck) {
        PatrolEnvCheckMapper mapper = factory.getMapper(PatrolEnvCheckMapper.class);
        return mapper.update(patrolEnvCheck);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        PatrolEnvCheckMapper mapper = factory.getMapper(PatrolEnvCheckMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public PatrolEnvCheckPo queryById(SessionFactory factory, String id) {

        PatrolEnvCheckMapper mapper = factory.getMapper(PatrolEnvCheckMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<PatrolEnvCheckPo> queryList(SessionFactory factory, PatrolEnvCheckPo patrolEnvCheck) {

        PatrolEnvCheckMapper mapper = factory.getMapper(PatrolEnvCheckMapper.class);

        List<PatrolEnvCheckPo> list = mapper.queryList(patrolEnvCheck);

        return list;
    }

    @Override
    @TaskAnnotation("queryByPatrolPointId")
    public List<PatrolEnvCheckPo> queryByPatrolPointId(SessionFactory factory, String patrolPointId) {
        PatrolEnvCheckPo patrolEnvCheckPo = new PatrolEnvCheckPo();
        patrolEnvCheckPo.setPatrolPointId(patrolPointId);
        return queryList(factory, patrolEnvCheckPo);
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, PatrolEnvCheckPo patrolEnvCheck, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        PatrolEnvCheckMapper mapper = factory.getMapper(PatrolEnvCheckMapper.class);

        List<PatrolEnvCheckPo> list = mapper.queryList(patrolEnvCheck);

        return new PageInfo(list);
    }

}